package com.github.phoenics.militia.bazookaFx.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.phoenics.militia.bazookaFx.dao.SchemaTablesViewDao;
import com.github.phoenics.militia.bazookaFx.domain.entity.Entity;
import com.github.phoenics.militia.bazookaFx.domain.entity.SchemaTablesView;
import com.github.phoenics.militia.bazookaFx.model.AppModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author Phoenics Chow
 * on 2019年01月31日
 */
@Service
public class SchemaTablesViewService  extends ServiceImpl<SchemaTablesViewDao, SchemaTablesView> {
    private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(SchemaTablesViewService.class);
    @Autowired
    SchemaTableColumnViewService schemaTableColumnViewService;
    @Autowired
    EntityService entityService;

    public List<SchemaTablesView> listBySchemaTable(String schemaTable){
        QueryWrapper<SchemaTablesView> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("table_schema",schemaTable);
        return this.list(queryWrapper);
    }

    @Transactional
    public void syncData(AppModel appModel){
        entityService.deleteAllEntities(appModel.getId());
        this.listBySchemaTable(appModel.getSchemaName()).forEach(r->{
            Entity entity=new Entity();
            entity.setTableName(r.getTableName());
            // insert entity


            entityService.save(entityService.reCfgEntity(entity));
            schemaTableColumnViewService.columsInTable(appModel.getSchemaName(),r.getTableName()).forEach(column->{

            });




        });


    }
    public void sampleBuilder(AppModel appModel){

    }


}
